<html><head><title>EntityAnimation3.pb</title></head>
<body bgcolor="#EFF1F0" link="#3A3966" vlink="#000000" alink="#000000">
<pre><font face="Courier New, Courier, mono"size="2"><font color="#3A3966">;
</font><font color="#3A3966">; ------------------------------------------------------------
</font><font color="#3A3966">;
</font><font color="#3A3966">;   PureBasic - Animation
</font><font color="#3A3966">;
</font><font color="#3A3966">;    (c) Fantaisie Software
</font><font color="#3A3966">;
</font><font color="#3A3966">; ------------------------------------------------------------
</font><font color="#3A3966">;
</font><font color="#3A3966">;Cursor = Move Robot
</font><font color="#3A3966">;Speed animation = PageUp and PageDown 
</font>
<b><font color="#3A3966">IncludeFile</font></b> <font color="#924B72">#PB_Compiler_Home</font> + &quot;examples/3d/Screen3DRequester.pb&quot;

<font color="#924B72">#ANIM_FADE_SPEED</font> = 7.5  <font color="#3A3966">; animation crossfade speed in % of full weight per second
</font>
<b><font color="#3A3966">Enumeration</font></b> 1
  <font color="#924B72">#Idle</font>
  <font color="#924B72">#Walk</font>
<b><font color="#3A3966">EndEnumeration</font></b>

<font color="#924B72">#NUM_ANIMS</font> = 2 <font color="#3A3966">; number of animations
</font>
<b><font color="#3A3966">Define</font></b>.f KeyX, KeyY, MouseX, MouseY, Angle, Speed = 1.0, TimeSinceLastFrame 
<b><font color="#3A3966">Global</font></b> RobotMove, FadeIn, FadeOut, Anim

<b><font color="#3A3966">Global</font></b> <b><font color="#3A3966">Dim</font></b> <font color="#3A3966">Anim</font>.s(<font color="#924B72">#NUM_ANIMS</font>)

<b><font color="#3A3966">Declare</font></b> <font color="#3A3966">fadeAnimations</font>(deltaTime.f)
<b><font color="#3A3966">Declare</font></b>.f <font color="#3A3966">CurveAngle</font>(Actuelle.f, Target.f, P.f)
<b><font color="#3A3966">Declare</font></b>.f <font color="#3A3966">WrapPi</font>(Angle.f)

<b><font color="#3A3966">Macro</font></b> <font color="#3A3966">Clamp</font>(num, min, max)
  <b><font color="#3A3966">If</font></b> num&lt;min
    num=min
  <b><font color="#3A3966">ElseIf</font></b> num&gt;max
    num=max
  <b><font color="#3A3966">EndIf</font></b>  
<b><font color="#3A3966">EndMacro</font></b>

<b><font color="#3A3966">If</font></b> <font color="#3A3966">InitEngine3D</font>()
  
<font color="#3A3966">  Add3DArchive</font>(<font color="#924B72">#PB_Compiler_Home</font> + &quot;examples/3d/Data/Textures&quot;        , <font color="#924B72">#PB_3DArchive_FileSystem</font>)
<font color="#3A3966">  Add3DArchive</font>(<font color="#924B72">#PB_Compiler_Home</font> + &quot;examples/3d/Data/Models&quot;          , <font color="#924B72">#PB_3DArchive_FileSystem</font>)
<font color="#3A3966">  Add3DArchive</font>(<font color="#924B72">#PB_Compiler_Home</font> + &quot;examples/3d/Data/Scripts&quot;         , <font color="#924B72">#PB_3DArchive_FileSystem</font>)
<font color="#3A3966">  Add3DArchive</font>(<font color="#924B72">#PB_Compiler_Home</font> + &quot;examples/3d/Data/Packs/desert.zip&quot;, <font color="#924B72">#PB_3DArchive_Zip</font>)
<font color="#3A3966">  Parse3DScripts</font>()
  
<font color="#3A3966">  InitSprite</font>()
<font color="#3A3966">  InitKeyboard</font>()
<font color="#3A3966">  InitMouse</font>()
  
  <b><font color="#3A3966">If</font></b> <font color="#3A3966">Screen3DRequester</font>()
    
<font color="#3A3966">    WorldShadows</font>(<font color="#924B72">#PB_Shadow_Modulative</font>, 0,<font color="#3A3966"> RGB</font>(180, 180, 255))
    
    <font color="#3A3966">;Ground
</font>    <font color="#3A3966">;
</font><font color="#3A3966">    CreateMaterial</font>(0,<font color="#3A3966"> LoadTexture</font>(0, &quot;Dirt.jpg&quot;))
<font color="#3A3966">    CreatePlane</font>(0, 1500, 1500, 40, 40, 15, 15)
<font color="#3A3966">    CreateEntity</font>(0,<font color="#3A3966">MeshID</font>(0),<font color="#3A3966">MaterialID</font>(0))
<font color="#3A3966">    EntityRenderMode</font>(0, 0)
    
    <font color="#3A3966">;Mesh
</font>    <font color="#3A3966">;
</font><font color="#3A3966">    LoadMesh</font>(1, &quot;robot.mesh&quot;)
    
    <font color="#3A3966">; Entity
</font>    <font color="#3A3966">;
</font><font color="#3A3966">    CreateEntity</font>(1,<font color="#3A3966"> MeshID</font>(1), <font color="#924B72">#PB_Material_None</font>, 0, 0, -50)
<font color="#3A3966">    EntityAnimationBlendMode</font>(1, <font color="#924B72">#PB_EntityAnimation_Average</font>)
    
    <font color="#3A3966">; Animation
</font>    <font color="#3A3966">;
</font>    animNames$ = &quot;Idle,Walk&quot;
    
    <font color="#3A3966">; populate our animation List
</font>    <b><font color="#3A3966">For</font></b> i = 1 <b><font color="#3A3966">To</font></b> <font color="#924B72">#NUM_ANIMS</font>
<font color="#3A3966">      Anim</font>(i) =<font color="#3A3966"> StringField</font>(animNames$, i, &quot;,&quot;)
      <font color="#3A3966">;EnableEntityAnimation(1, Anim(i), #True, #False)
</font>      Fadein  = <font color="#924B72">#False</font>
      FadeOut = <font color="#924B72">#False</font>
    <b><font color="#3A3966">Next</font></b>
    
    <font color="#3A3966">; SkyBox
</font>    <font color="#3A3966">;
</font><font color="#3A3966">    SkyBox</font>(&quot;Desert07.jpg&quot;)
    
    <font color="#3A3966">; Camera
</font>    <font color="#3A3966">;
</font><font color="#3A3966">    CreateCamera</font>(0, 0, 0, 100, 100)
<font color="#3A3966">    MoveCamera</font>(0, 50, 100, 180, <font color="#924B72">#PB_Absolute</font>)
<font color="#3A3966">    CameraLookAt</font>(0,<font color="#3A3966"> EntityX</font>(1),<font color="#3A3966"> EntityY</font>(1) + 40,<font color="#3A3966"> EntityZ</font>(1))
    
    
<font color="#3A3966">    CreateLight</font>(0,<font color="#3A3966"> RGB</font>(255, 255, 255), -40, 100, 80)
<font color="#3A3966">    AmbientColor</font>(<font color="#3A3966">RGB</font>(80, 80, 80))
    
    <b><font color="#3A3966">Repeat</font></b>
<font color="#3A3966">      Screen3DEvents</font>()
      
      <b><font color="#3A3966">If</font></b> <font color="#3A3966">ExamineMouse</font>()
        MouseX = -<font color="#3A3966">MouseDeltaX</font>()/10 
        MouseY = -<font color="#3A3966">MouseDeltaY</font>()/10
      <b><font color="#3A3966">EndIf</font></b>
      
      RobotMove = <font color="#924B72">#False</font>    
      Angle =<font color="#3A3966"> EntityYaw</font>(1)
      <b><font color="#3A3966">If</font></b> <font color="#3A3966">ExamineKeyboard</font>()
        
        <b><font color="#3A3966">If</font></b> <font color="#3A3966">KeyboardPushed</font>(<font color="#924B72">#PB_Key_Left</font>)
<font color="#3A3966">          MoveEntity</font>(1, -1 * Speed, 0, 0)
          Angle = 180
          RobotMove = <font color="#924B72">#True</font>
          
        <b><font color="#3A3966">ElseIf</font></b> <font color="#3A3966">KeyboardPushed</font>(<font color="#924B72">#PB_Key_Right</font>)
<font color="#3A3966">          MoveEntity</font>(1, 1 * Speed, 0, 0)
          Angle = 0 
          RobotMove = <font color="#924B72">#True</font>
          
        <b><font color="#3A3966">ElseIf</font></b> <font color="#3A3966">KeyboardPushed</font>(<font color="#924B72">#PB_Key_Up</font>)
<font color="#3A3966">          MoveEntity</font>(1, 0, 0, -1 * Speed)
          Angle = 90 
          RobotMove = <font color="#924B72">#True</font>
          
        <b><font color="#3A3966">ElseIf</font></b> <font color="#3A3966">KeyboardPushed</font>(<font color="#924B72">#PB_Key_Down</font>)
<font color="#3A3966">          MoveEntity</font>(1, 0, 0, 1 * Speed)
          Angle = -90
          RobotMove = <font color="#924B72">#True</font>
        <b><font color="#3A3966">EndIf</font></b>
        
        <b><font color="#3A3966">If</font></b> <font color="#3A3966">KeyboardPushed</font>(<font color="#924B72">#PB_Key_PageUp</font>) <b><font color="#3A3966">And</font></b> Speed &lt; 1.0
          Speed + 0.05
        <b><font color="#3A3966">ElseIf</font></b> <font color="#3A3966">KeyboardPushed</font>(<font color="#924B72">#PB_Key_PageDown</font>) <b><font color="#3A3966">And</font></b> Speed &gt; 0.1 
          Speed - 0.05
        <b><font color="#3A3966">EndIf</font></b>
        
      <b><font color="#3A3966">EndIf</font></b>
      
<font color="#3A3966">      RotateEntity</font>(1, 0,<font color="#3A3966"> CurveAngle</font>(<font color="#3A3966">EntityYaw</font>(1), Angle, 4 * TimeSinceLastFrame), 0)
      
      <b><font color="#3A3966">If</font></b> RobotMove
        <b><font color="#3A3966">If</font></b> Anim &lt;&gt; <font color="#924B72">#Walk</font>
          Anim = <font color="#924B72">#Walk</font>
          Fadein  = <font color="#924B72">#Walk</font>
          FadeOut = <font color="#924B72">#Idle</font> 
<font color="#3A3966">          StartEntityAnimation</font>(1,<font color="#3A3966"> Anim</font>(Anim), <font color="#924B72">#PB_EntityAnimation_Manual</font>)
        <b><font color="#3A3966">EndIf</font></b>  
      <b><font color="#3A3966">Else</font></b>
        <b><font color="#3A3966">If</font></b> Anim &lt;&gt; <font color="#924B72">#Idle</font>
          Anim = <font color="#924B72">#Idle</font>
          Fadein  = <font color="#924B72">#Idle</font>
          FadeOut = <font color="#924B72">#Walk</font> 
<font color="#3A3966">          StartEntityAnimation</font>(1,<font color="#3A3966"> Anim</font>(Anim), <font color="#924B72">#PB_EntityAnimation_Manual</font>)
        <b><font color="#3A3966">EndIf</font></b>  
      <b><font color="#3A3966">EndIf</font></b>  
      
<font color="#3A3966">      AddEntityAnimationTime</font>(1,<font color="#3A3966"> Anim</font>(Anim), TimeSinceLastFrame)
<font color="#3A3966">      fadeAnimations</font>(TimeSinceLastFrame / 1000.0)
      
<font color="#3A3966">      RotateCamera</font>(0, MouseY, MouseX, 0, <font color="#924B72">#PB_Relative</font>)
      
      TimeSinceLastFrame =<font color="#3A3966"> RenderWorld</font>() * Speed
      
<font color="#3A3966">      FlipBuffers</font>()
    <b><font color="#3A3966">Until</font></b> <font color="#3A3966">KeyboardPushed</font>(<font color="#924B72">#PB_Key_Escape</font>) <b><font color="#3A3966">Or</font></b> Quit = 1
  <b><font color="#3A3966">EndIf</font></b>
  
<b><font color="#3A3966">Else</font></b>
<font color="#3A3966">  MessageRequester</font>(&quot;Error&quot;, &quot;The 3D Engine can't be initialized&quot;,0)
<b><font color="#3A3966">EndIf</font></b>

<b><font color="#3A3966">End</font></b>

<b><font color="#3A3966">Procedure</font></b> <font color="#3A3966">fadeAnimations</font>(deltaTime.f)
  <b><font color="#3A3966">Protected</font></b>.f newWeight
  
  <b><font color="#3A3966">If</font></b> FadeIn
    
    <font color="#3A3966">; slowly fade this animation in Until it has full weight
</font>    newWeight =<font color="#3A3966"> GetEntityAnimationWeight</font>(1,<font color="#3A3966"> Anim</font>(FadeIn)) + deltaTime * <font color="#924B72">#ANIM_FADE_SPEED</font>
<font color="#3A3966">    Clamp</font>(newWeight, 0, 1)
<font color="#3A3966">    SetEntityAnimationWeight</font>(1,<font color="#3A3966"> Anim</font>(FadeIn), newWeight)
    <b><font color="#3A3966">If</font></b> newWeight &gt;= 1 
      FadeIn = <font color="#924B72">#False</font>
    <b><font color="#3A3966">EndIf</font></b>  
    
  <b><font color="#3A3966">EndIf</font></b>
  
  <b><font color="#3A3966">If</font></b> FadeOut
    
    <font color="#3A3966">; slowly fade this animation out Until it has no weight, And then disable it
</font>    newWeight.f =<font color="#3A3966"> GetEntityAnimationWeight</font>(1,<font color="#3A3966"> Anim</font>(FadeOut)) - deltaTime * <font color="#924B72">#ANIM_FADE_SPEED</font>
<font color="#3A3966">    Clamp</font>(newWeight, 0, 1)
<font color="#3A3966">    SetEntityAnimationWeight</font>(1,<font color="#3A3966"> Anim</font>(FadeOut), newWeight)
    <b><font color="#3A3966">If</font></b> newWeight &lt;= 0
<font color="#3A3966">      StopEntityAnimation</font>(1,<font color="#3A3966"> Anim</font>(FadeOut))
      FadeOut = <font color="#924B72">#False</font>
    <b><font color="#3A3966">EndIf</font></b>
  <b><font color="#3A3966">EndIf</font></b>
  
<b><font color="#3A3966">EndProcedure</font></b>

<b><font color="#3A3966">Procedure</font></b>.f <font color="#3A3966">WrapPi</font>(Angle.f) 
  Angle + 180
  Angle -<font color="#3A3966"> Round</font>(Angle * 1/360.0, <font color="#924B72">#PB_Round_Down</font>) * 360
  Angle - 180
  <b><font color="#3A3966">ProcedureReturn</font></b> Angle
<b><font color="#3A3966">EndProcedure</font></b>

<b><font color="#3A3966">Procedure</font></b>.f <font color="#3A3966">CurveAngle</font>(Actuelle.f, Target.f, P.f)
  Delta.f =<font color="#3A3966"> WrapPi</font>(Target-Actuelle)
  <b><font color="#3A3966">If</font></b> P &gt; 1000 : P = 1000 : <b><font color="#3A3966">EndIf</font></b>
  Valeur.f = Actuelle + (Delta * P / 1000)
  <b><font color="#3A3966">ProcedureReturn</font></b> <font color="#3A3966">WrapPi</font>(Valeur)
<b><font color="#3A3966">EndProcedure</font></b> </font></pre>
</body></html>
